package com.melody.android.ui.component

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.melody.android.R
import com.melody.android.databinding.BottomSheetCreateSongListBinding
import com.melody.android.logic.model.SongList
import com.melody.android.ui.viewModel.SongViewModel

class BottomSheetCreateSongList : BottomSheetDialogFragment() {

    private lateinit var binding: BottomSheetCreateSongListBinding  // 声明视图绑定变量
    private lateinit var songViewModel: SongViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = BottomSheetCreateSongListBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 获取MainActivity所持有的SongViewModel
        songViewModel = ViewModelProvider(requireActivity())[SongViewModel::class.java]
        initView()
    }

    private fun initView() {
        binding.btnConfirm.setOnClickListener {
            val songList = SongList(
                R.drawable.img_song_cover_yhm,
                binding.editSongListName.text.toString(),
                0
            )
            songViewModel.addSongList(songList)     // 添加自定义歌单

            dismiss()                               // 隐藏BottomSheet
            binding.editSongListName.text?.clear()  // 清空输入框
        }
    }

    companion object {
        const val TAG = "ModalBottomSheet"
    }

}